#include "queue.h"

struct Queue* createQueue(int capacity,
						  void* (*_cpData)(void*, const void*),
						  void* (*_getElePointer)(void*_array, int step),
						  void (*_printEle)(void*),
						  int (*_dataSize)())
{
	assert(capacity>0);
	struct Queue* _queue = calloc(sizeof(struct Queue), 1);
	if(_queue==NULL)
		return NULL;

	_queue->_cpData = _cpData;
	_queue->_getElePointer = _getElePointer;
	_queue->_printEle = _printEle;
	_queue->_dataSize = _dataSize;
	_queue->capacity = capacity;
	_queue->data = calloc(_queue->_dataSize(), capacity);
	_queue->front = 0;
	_queue->back = -1;
	
	if(_queue->data==NULL)
	{
		free(_queue);
		return NULL;
	}

	return _queue;
}

void deleteQueue(struct Queue* _queue)
{
	if(_queue==NULL)
		return;

	free(_queue->data);
	free(_queue);
	return;
}

void clearQueue(struct Queue* _queue)
{
	if(_queue==NULL)
		return;
	_queue->currentSize = 0;
	_queue->front = _queue->back = -1;
	return ;
}

int pushEle(struct Queue* _queue, void* _el)
{
	if(_queue==NULL||_el==NULL||_queue->currentSize==_queue->capacity)
		return 0;
	
	_queue->back = (++_queue->back)>(_queue->capacity-1)?0:_queue->back;
	_queue->_cpData(_queue->_getElePointer(_queue->data, _queue->back), _el);
	_queue->currentSize++;
	return 1;
}
int popEle(void* _dst, struct Queue* _queue)
{
	if(_queue==NULL||_queue->currentSize==0||_dst==NULL)
		return 0;

	_queue->_cpData(_dst, _queue->_getElePointer(_queue->data, _queue->front));
	_queue->front = (++_queue->front)>(_queue->capacity-1)?0:_queue->front;
	_queue->currentSize--;
	return 1;
}


void printQueueInfo(struct Queue* _queue)
{

}



